package com.jack.wlbletest;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.function.Consumer;

public class BleAdapter extends RecyclerView.Adapter<BleAdapter.ViewHolder> {
    private static final String TAG = BleAdapter.class.getName();
    private ArrayList<BluetoothDevice> list;
    private BleAdapterClickListener listener;

    public interface BleAdapterClickListener {
        void onItemClick(int index);
    }

    public void setListener(BleAdapterClickListener listener) {
        this.listener = listener;
    }

    public BleAdapter() {
        super();
        list = new ArrayList<>();
    }

    public void clearData() {
        list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ble_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String name;

        try {
            name = list.get(position).getName();
        }catch (SecurityException e) {
            e.printStackTrace();
            name = "empty";
        }

        if (list.size() > 0) {
            final int index = position;
            holder.getTextView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.onItemClick(index);
                    }
                }
            });

            if (name == null) {
                name = "empty";
            }
            holder.getTextView().setText(name);
        }
    }

    public BluetoothDevice getItem(int index) {
        return list.get(index);
    }

    public void addItem(BluetoothDevice device) {
        final int index[] = new int[]{-1};
        boolean found = false;
        String name;

        try {
            name = device.getName();
        }catch (SecurityException e) {
            e.printStackTrace();
            name = "empty";
        }

        for(int i = 0; i < list.size(); i++) {
            Log.d(TAG, "addItem: name:" + name + ":" + device.getAddress());
            Log.d(TAG, "list item" + list.get(i).getAddress());
            if (list.get(i).getAddress().equals(device.getAddress())) {
                index[0] = list.indexOf(device);
                found = true;
                break;
            }
        }

        Log.d(TAG, "addItem: list size:" + list.size());
        Log.d(TAG, "addItem: name:" + name);
        Log.d(TAG, "addItem: index:" + index[0]);

        if (!found) {
            Log.d(TAG, "add device into list");
            list.add(device);
            notifyItemChanged(list.indexOf(device));
        }

    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }

        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView name;

        public ViewHolder(View view) {
            super(view);

            name = (TextView) view.findViewById(R.id.blename);
        }

        public TextView getTextView() {
            return name;
        }
    }
}
